﻿using System;
using System.Numerics;
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using PodpisyElektorniczne.Classes;

namespace PodpisyElektorniczne.Interfaces
{
    public class KeyPreferences 
    {
        //private RSAKeysProperties _rsaProperties;
        public KeyPreferences()
        {
            IsGenerated = 0;
            LengthOrderZp = 16;
            LengthOrderZq = 16;
            NoTrials = 10;
            NoMembers = 1;
            IsRandom = false;
        }

        public int NoTrials { get; set; }
        public int NoMembers { get; set; }
        public int LengthOrderZp { get; set; }
        public int LengthOrderZq { get; set; }
        public short IsGenerated { get; set; }
        public bool IsRandom { get; set; }
        //to implement
        public static bool ObjSerialization(KeyPreferences ke)
        {
            try
            {
                using (var storage = IsolatedStorageFile.GetMachineStoreForAssembly())
                {
                    if(storage.FileExists(Settings.SignatureSettings))storage.DeleteFile(Settings.SignatureSettings);
                    using (
                    var stream = new IsolatedStorageFileStream(Settings.SignatureSettings, FileMode.OpenOrCreate,
                                                               storage))
                using (var writer = XmlWriter.Create(stream))
                {
                    new XmlSerializer(typeof (KeyPreferences)).Serialize(writer, ke);
                    return true;
                }
                }
            }
            catch(Exception)
            {
                throw new Exception(@"There is a sirious problem with storing settings for entire schema. Try again.");
            }
        }

        public static KeyPreferences ObjDeserializer()
        {
            try
            {

                using (var storage = IsolatedStorageFile.GetMachineStoreForAssembly())
                if (storage.FileExists(Settings.SignatureSettings))
                {
                    using (
                        var stream = new IsolatedStorageFileStream(Settings.SignatureSettings, FileMode.Open,
                                                                    storage))
                    using (var reader = XmlReader.Create(stream))
                    {
                        return (KeyPreferences) new XmlSerializer(typeof (KeyPreferences)).Deserialize(reader);
                    }
                }
                return default(KeyPreferences);
            }
            catch(Exception)
            {
                throw new Exception(@"(KeyPreference)There is a problem with getting settings from IS. try again or set it again.");
            }
        }

        public string[] ToStringArray()
        {
            return new[]
                       {IsGenerated.ToString(), LengthOrderZp.ToString(), LengthOrderZq.ToString(), NoMembers.ToString(),
                           NoTrials.ToString(), IsRandom.ToString()};
        }
    }
}
